﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using Service;
using MODEL;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using System.Windows.Input;
using Microsoft.Practices.ServiceLocation;
using GalaSoft.MvvmLight.Views;
using GalaSoft.MvvmLight.Ioc;
using Common;


namespace ViewModel.SystemManagement
{
    public class UserManagerViewModel:ViewModelBase
    {
        public ICommand AddNewUserCommand { get; set; }
        public ICommand DeleteUserCommand { get; set; }
        public ICommand UserRoleManageCommand { get; set; }
        public ICommand RefreshCommand { get; set; }

        private string m_AutoCompleteInput;
        public string AutoCompleteInput
        {
            get { return this.m_AutoCompleteInput; }
            set { this.m_AutoCompleteInput = value; base.RaisePropertyChanged(() => this.AutoCompleteInput); }
        }

        private IUserService m_UserService;
        private IRoleService m_RoleService;

        public UserManagerViewModel(IUserService userService, IRoleService roleService)
        {
            this.m_UserService = userService;
            this.m_RoleService = roleService;
            this.AddNewUserCommand = new RelayCommand(ShowAddUserForm);
            this.UserRoleManageCommand = new RelayCommand<User>(ShowUserRoleManageForm);
            this.DeleteUserCommand = new RelayCommand<int>(DeleteUser);
            this.RefreshCommand = new RelayCommand(RefreshData);

            RefreshData();
        }
        private IEnumerable<User> m_UserList;
        public IEnumerable<User> UserList
        {
            get { return m_UserList; }
            set { m_UserList = value;RaisePropertyChanged(() => UserList); }
        }

        public async void RefreshData()
        {
            UserQueryCriteria criteria = new UserQueryCriteria();
            if (!string.IsNullOrWhiteSpace(this.AutoCompleteInput))
            {
                criteria.AutoCompleteInput = this.AutoCompleteInput;
            }
            UserList = await this.m_UserService.GetUsersAsync(criteria);
        }

        public void ShowUserRoleManageForm(User user)
        {
            Role role = this.m_RoleService.QueryRole(user.Id).Result.FirstOrDefault();

            user.role = role;

            UserRoleManageViewModel vm = SimpleIoc.Default.GetInstanceWithoutCaching<UserRoleManageViewModel>();
            vm.User = user;

            this.MessengerInstance.Send<ViewModelBase>(vm, "UserManager_ShowRoleManagerForm");
        }

        public void ShowAddUserForm()
        {
            UserInsertViewModel vm = SimpleIoc.Default.GetInstanceWithoutCaching<UserInsertViewModel>();
            vm.ParentViewModel = this;
            this.MessengerInstance.Send<ViewModelBase>(vm, "UserManager_ShowInsertForm");
        }

        public async void DeleteUser(int userId)
        {
            try
            {
                bool IsConfirm = await ServiceLocator.Current.GetInstance<IDialogService>().ShowMessage("当前数据即将被删除，是否确认？", "提示", "确认", "取消", null);
                if (!IsConfirm)
                {
                    return;
                }
                Task task = this.m_UserService.DeleteUser(userId);
                task.Wait();
                await ServiceLocator.Current.GetInstance<IDialogService>().ShowMessage("删除成功", "提示");
                RefreshData();
            }
            catch (Exception ex)
            {
                await ServiceLocator.Current.GetInstance<IDialogService>().ShowError("删除失败", "错误", "确定", null);
                Log.WriteException(ex);
            }
        }
    }
}
